package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.TopicDao;
import org.whuims.pois.dao.UserDao;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.SiteC;
import org.whuims.pois.model.SpiderC;
import org.whuims.pois.model.TopicC;
import org.whuims.pois.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zhongwei on 2014/5/14.
 */
public class TopicAction extends DispatchAction {
    BaseDao baseDao = new BaseDao();
    TopicDao topicDao = new TopicDao();
    UserDao userDao = new UserDao();

    public ActionForward show(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        int privilege = (Integer) session.getAttribute("privilege");
        if (privilege == 1) {
            request.setAttribute("errormessage", "对不起，您尚未获得该权限！");
            return mapping.findForward("error");
        } else {
            List<TopicC> list = topicDao.select();
            request.setAttribute("result", list);
            long topicId = getTopic(request);
            if (topicId <= 0) {
                return mapping.findForward("login");
            }
            request.getSession().setAttribute("topicId", topicId);
            request.setAttribute("topicId", topicId);
            return mapping.findForward("show");
        }

    }

    public ActionForward addtopic2user(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("userName");
        String topic = request.getParameter("topicId");
        Long topicId = Long.parseLong(topic);
        User user = userDao.findByName(userName);
        if (user == null) {
            return mapping.findForward("login");
        }
        user.setTopicid(topicId);
        session.setAttribute("topicId", topicId);
        TopicC topicC = (TopicC) baseDao.getByPk(TopicC.class, topicId);
        session.setAttribute("topicName", topicC.getName());    //更新主题名称
        userDao.modify(user);
        request.setAttribute("topicId", user.getTopicid()); //更新主题ID
        int languagecount = baseDao.query("select distinct planguage from DataC where topicid='" + topicId + "'").size();
        session.setAttribute("languagecount", languagecount);
        return mapping.findForward("success");
    }

    public ActionForward insert(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TopicC bean = (TopicC) form;
//        bean.setName(Chinese.toChinese(bean.getName()));
//        bean.setDescription(Chinese.toChinese(bean.getDescription()));
//        bean.setKeywords(Chinese.toChinese(bean.getKeywords()));
//        bean.setExcludeWords(Chinese.toChinese(bean.getExcludeWords()));
        String topic = bean.getName();
        if (topic.trim().equals("")) {
            request.setAttribute("errormessage", "主题词不能为空");
            return mapping.findForward("error");
        }

        if (baseDao.getTotalCounts("from TopicC where name=?", topic) > 0) {
            request.setAttribute("errormessage", "主题词已经存在");
            return mapping.findForward("error");
        }
        bean.setId(0);
        String keyword = bean.getKeywords();
        if (keyword.contains("，")) {
            keyword = keyword.replaceAll("，", ",");
        }
        bean.setKeywords(keyword);
        topicDao.insert(bean);
        TopicC topicC = (TopicC) baseDao.find("from TopicC where name='"+bean.getName()+"'").get(0);
        addTopicSpider(topicC);
        return mapping.findForward("success");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        String topicId = request.getParameter("topicId");
        Long id = Long.parseLong(topicId);
        TopicC bean = topicDao.select(id);
        request.setAttribute("bean", bean);
        return mapping.findForward("edit");
    }

    public ActionForward modify(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TopicC bean = (TopicC) form;
        if (bean.getName().trim().equals("")) {
            request.setAttribute("errormessage", "主题词不能为空");
            return mapping.findForward("error");
        }

        topicDao.modify(bean);
        request.getSession().setAttribute("topicName", bean.getName());//修改当前主题名称
        return mapping.findForward("success");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        String topicId = request.getParameter("topicId");
        Long id = Long.parseLong(topicId);
        topicDao.delete(id);
        deleteTopicSpider(id);
        return mapping.findForward("success");
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }

    private void addTopicSpider(TopicC bean) {
        List<SiteC> siteCs = baseDao.find("from SiteC");
        for (SiteC siteC : siteCs) {
            SpiderC spiderC = new SpiderC();
            spiderC.setSite(spiderC.getId());
            spiderC.setTopicId(bean.getId());
            spiderC.setSitename(siteC.getSiteName());
            spiderC.setStatus(0);
            spiderC.setLimitdatetype(0);
            spiderC.setCrawlertype(0);
            spiderC.setPausetype(2);
            spiderC.setPauseextras("5");
            spiderC.setSite(siteC.getId());
            baseDao.create(spiderC);
        }
    }

    private void deleteTopicSpider(long topicId) {
        List<SpiderC> spiderCs = baseDao.find("from SpiderC where topicId=" + topicId + "");
        for (SpiderC spiderC : spiderCs) {
            baseDao.delete(spiderC);
        }
    }
}

